<form action="${ctx}/channel/manageform" method="post" id="addChannel" enctype="multipart/form-data" class="change-error-two-box">
		<input type="hidden" name="channelId" value="${channel.channelId!}" />
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>线路类型：</label>
				<#if channel.channelType?? && channel.channelType == 1><label class="radio-inline">SIM网关</label></#if>
				<#if channel.channelType?? && channel.channelType == 2><label class="radio-inline">SIP线路</label></#if>
				<input type="hidden" name="channelType"  value="${channel.channelType!}" />
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6 col-select179-box">
				<label><span style="color: red">*</span>网关标识：</label>
				<select class="form-control select2" name="voxGatewayName" required >
					<option value="">--请选择--</option>
					<#list gateways as a>
						<option value="${a.gwName}" <#if channel.voxGatewayName?? && channel.voxGatewayName == a.gwName>selected</#if> >${a.gwName}</option>
					</#list>
				</select>
			</div>
			<div class="col-sm-6">
				<label><span style="color: red">*</span>网关IP：</label>
				<input class="form-control" type="text" name="gatewayIp" value="${channel.gatewayIp!}"  required maxlength="50" />
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>外呼号码:</label>
				<input class="form-control" type="text" name="channelName" value="${channel.channelName!}" maxlength="30" />
			</div>
			<div class="col-sm-6">
				<label><span style="color: red">*</span>IP/TEL:</label>
				<input class="form-control" type="text" name="channelNum" value="${channel.channelNum!}" maxlength="30" />
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>录音地址:</label>
				<input class="form-control" type="text" name="voxServerAddr" value="${channel.voxServerAddr!}"  required maxlength="200" />
			</div>
			<div class="col-sm-6">
				<label>网关api_ip:</label>
				<input class="form-control" type="text" name="gatewayApiIp" value="${channel.gatewayApiIp!}" maxlength="50" />
			</div>
		</div>
		<#-- 下面的新增时，或SIM卡编辑时时才显示 -->
		<#if channel.channelType?? && (channel.channelType == 1)>
			<div class="form-group form-inline row">
				<div class="col-sm-6">
					<label><span style="color: red">*</span>卡槽号:</label>
					<select class="form-control" name="port" required >
						<option value="">--请选择--</option>
						<#list 1 .. 32 as i>
							<option value="${i}" <#if channel.port?? && channel.port == i>selected</#if> >${i}</option>
						</#list>
					</select>
				</div>
			</div>
			<div class="form-group form-inline row">
				<div class="col-sm-6">
					<label>SIM卡A:</label>
					<input class="form-control" type="text" name="simAPhone" value="${channelPhoneMap["1"]!}" placeholder="请输入外呼号码" maxlength="30" />
				</div>
				<div class="col-sm-6">
					<label>SIM卡B:</label>
					<input class="form-control" type="text" name="simBPhone" value="${channelPhoneMap["2"]!}" placeholder="请输入外呼号码" maxlength="30" />
				</div>
			</div>
			<div class="form-group form-inline row">
				<div class="col-sm-6">
					<label>SIM卡C:</label>
					<input class="form-control" type="text" name="simCPhone" value="${channelPhoneMap["3"]!}" placeholder="请输入外呼号码" maxlength="30" />
				</div>
				<div class="col-sm-6">
					<label>SIM卡D:</label>
					<input class="form-control" type="text" name="simDPhone" value="${channelPhoneMap["4"]!}" placeholder="请输入外呼号码" maxlength="30" />
				</div>
			</div>
		</#if>
		
		<#-- 下面的新增时，或SIP线路编辑时时才显示 -->
		<#if channel.channelType?? && (channel.channelType == 2)>
			<div class="form-group form-inline row">
				<div class="col-sm-6">
					<label><span style="color: red">*</span>并发数：</label>
					<input class="form-control" type="text" name="concurrentNum" value="${channel.concurrentNum!}" maxlength="10" required digits="true" />
				</div>
				<div class="col-sm-6">
					<label><span style="color: red">*</span>AI比例：</label>
					<select class="form-control" name="robotRatio" id="robotRatio" style="width: 120px;" required >
						<option value="">--请选择--</option>
						<#list robotRatioList as a>
							<option value="${a}" <#if channel.robotRatio?? && a==channel.robotRatio?c>selected</#if> >${a}</option>
						</#list>
					</select>
				</div>
			</div>
		</#if>
		
		
		<h4 style="padding: 20px 0;border-bottom: 1px solid #e5e5e5">计费信息：</h4>
		<div class="form-group form-inline row">
			<div class="col-sm-6 company-name col-select179-box" >
				<label><span style="color: red">*</span>代理商：</label>
				<select class="form-control select2" name="agentId" id="agentId" required <#if channel.channelId?? && (loginUser.loginName != 'platadmin')>disabled</#if> >
					<option value="">--请选择--</option>
					<#list agentList as a>
						<option value="${a.id}" <#if channel.agentId ?? && channel.agentId == a.id>selected</#if> >${a.nickName}</option>
					</#list>
				</select>
				<input class="form-control" type="hidden" name="agentName" value="${channel.agentName!}" required />
			</div>
			<div class="col-sm-6 company-name col-select179-box">
				<label><span style="color: red">*</span>企业名称：</label>
				<select class="form-control select2" id="companyId" required <#if channel.channelId?? && (loginUser.loginName != 'platadmin')>disabled</#if> >
					<option value="">--请选择--</option>
					<#list companys as a>
						<option value="${a.companyId}" <#if channel.companyId ?? && channel.companyId == a.companyId>selected</#if> >${a.companyId}-${a.companyName}</option>
					</#list>
				</select>
				<input class="form-control" type="hidden" name="companyName" value="${channel.companyName!}" />
				<input class="form-control" type="hidden" name="companyId" value="${channel.companyId!}" />
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>计费：</label>    
				<select class="form-control" name="clearingForm" required <#if channel.clearingForm?? >disabled</#if> >
					<option value="">--请选择--</option>
					<#list clearingFormMap as key, value>
						<#if channel.clearingForm?exists && key == channel.clearingForm?string>
							<option value="${key}" selected="selected">${value}</option>
						<#else>
							<option value="${key}">${value}</option>
						</#if>
				   </#list>
				</select>
			</div>
			<div class="col-sm-6">
				<label><span style="color: red">*</span>使用月数：</label>
				<#if channel.monthsOfUse?? >
					<select class="form-control" disabled >
						<#list 1 .. 24 as i>
							<option value="${i}" <#if channel.monthsOfUse?? && channel.monthsOfUse == i>selected</#if> >${i}</option>
						</#list>
					</select>
					<#-- 添加一个隐藏域，方便后台计算机器人数 -->
					<input type="hidden" name="monthsOfUse" value="${channel.monthsOfUse!}" />
				<#else>
					<select class="form-control" name="monthsOfUse" required >
						<#list 1 .. 24 as i>
							<option value="${i}" <#if channel.monthsOfUse?? && channel.monthsOfUse == i>selected</#if> >${i}</option>
						</#list>
					</select>
				</#if>
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>订单号:</label>
				<input class="form-control" type="text" name="orderNum" value="${channel.orderNum!}" required <#if channel.orderNum?? >disabled</#if> />
			</div>
			<div class="col-sm-6">
				<label>是否启用：</label>
				<#if channel.channelStatus = 1>
					<label class="radio-inline"><input type="radio" value="0" disabled > 停用</label>
					<label class="radio-inline"><input type="radio" value="1" disabled checked > 启用</label>
					<input type="hidden" name="channelStatus" value="${channel.channelStatus!}" />
				<#else>
					<label class="radio-inline"><input type="radio" name="channelStatus" value="0" checked > 停用</label>
					<label class="radio-inline"><input type="radio" name="channelStatus" value="1" > 启用</label>
				</#if>
			</div>
		</div>
		<#-- <div class="form-group form-inline row">
			<div class="col-sm-6">
				<label>开始时间:</label>
				<#if channel.channelStatus = 1>
					<input class="form-control" type="text" name="beginTimeStr" readonly="readonly" <#if channel.beginTime??>value='${channel.beginTime?string("yyyy-MM-dd")}'</#if> />
				<#else>
					<input class="form-control" type="text" name="beginTimeStr" <#if channel.beginTime??>value='${channel.beginTime?string("yyyy-MM-dd")}'</#if> />
				</#if>
			</div>
			<div class="col-sm-6">
				<label>结束时间:</label>
				<label class="radio-inline" id="endTimeStr"><#if channel.endTime??>${channel.endTime?string("yyyy-MM-dd")}</#if></label>
			</div>
		</div> -->
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label>开始时间:</label>
				<label class="radio-inline" ><#if channel.beginTime??>${channel.beginTime?string("yyyy-MM-dd")}</#if></label>
			</div>
			<div class="col-sm-6">
				<label>结束时间:</label>
				<label class="radio-inline" ><#if channel.endTime??>${channel.endTime?string("yyyy-MM-dd")}</#if></label>
			</div>
		</div>
</form>

<script type="text/javascript">
	$(':input[name="beginTimeStr"]').datepicker({
		autoclose: true,
		format: 'yyyy-mm-dd',
		clearBtn: true,
	    language: 'zh-CN'
	});
	
	$("#addChannel").validate({
        rules: {
        	channelName: {
        		trimRequired: true,
                unContainSpace : true
            },
        	channelNum: {
        		trimRequired: true,
                unContainSpace : true,
                validateChannelNum : true
            },
            beginTimeStr : {
            	beginTimeRequired : true
            }
        },
        messages: {
			channelName: {
        		
        	},
        	channelNum: {
                required: "该字段不能为空"
            },
            beginTimeStr : {
            	beginTimeRequired : "启用时该字段不能为空"
            }
        }
	});
	
	// 返回true验证通过， 返回false验证不通过 
	$.validator.addMethod("validateChannelNum", function(value, element) {
		var channelId = $(":input[name='channelId']").val();
		var channelNum = $.trim($(":input[name='channelNum']").val());
		
		var verificationPassed = false;
		$.ajax({
			url : "${ctx}/channel/validateChannelNum",
			type : 'post',
			dataType : 'json',
			async : false,
			data : {"channelId" : channelId, "channelNum" : channelNum},
			success : function(data) {
				if (data.result == 'success') {
					verificationPassed = true;
				}
			}
		});
		
		return verificationPassed;
	}, "该字段不能为重复");
	
	// 返回true验证通过， 返回false验证不通过 
	$.validator.addMethod("beginTimeRequired", function(value, element) {
		var channelStatus = $(":input[name='channelStatus']:checked").val();
		// console.log("channelStatus="+channelStatus);
		var verificationPassed = true;
		if (channelStatus == 1 && value == '') {
			verificationPassed = false;
		}
		
		return verificationPassed;
	}, "启用时该字段不能为空");
	
	$('.select2').select2();
	$.fn.modal.Constructor.prototype.enforceFocus = function() {};

	
	/* 网关-录音地址 */
	var gateway = {
		<#list gateways as a>
			"${a.gwName}": "${a.voxServerAddr!}",
		</#list>
	};
	
	/* 代理商切换事件 */
	$(':input[name="agentId"]').change(function(){
		var agentId = $(this).val();
		var agentName = $(this).find("option:selected").text();
		$("input[name='agentName']").val(agentName);
		$(this).valid();
		
		/* 级联切换公司 */
		$("input[name='companyName']").val('');
		$("#companyId").html('<option value="">--请选择--</option>');
		$.ajax({
			url : "${ctx}/company/list4agentId",
			type : 'post',
			dataType : 'json',
			async : false,
			data : {"agentId" : agentId},
			success : function(data) {
				if (data.result == 'success') {
					for (var i = 0; i < data.data.length; i++) {
						$("#companyId").append('<option value="'+data.data[i].companyId+'">'+data.data[i].companyId+'-'+data.data[i].companyName+'</option>');
					}
				}
			}
		});
	});
	
	/* 公司的切换事件 */
	$("#companyId").change(function() {
		var companyName = $(this).find("option:selected").text();
		$("input[name='companyName']").val(companyName);
		$(this).valid();
	});
	
	/* 切换网关 */
	$(':input[name="voxGatewayName"]').change(function(){
		var key = $(this).val();
		/* 切换网关时，切换录音地址 */
		var voxServerAddr = gateway[''+key];
		$(":input[name='voxServerAddr']").val(voxServerAddr);
		$(this).valid();
	});
	
	/* 计算结束时间 */
// 	$(':input[name="beginTimeStr"], :input[name="monthsOfUse"]').change(function() {
// 		var monthsOfUse = $(":input[name='monthsOfUse']").val();
// 		var beginTimeStr = $(":input[name='beginTimeStr']").val();
// 		if (monthsOfUse == '' || beginTimeStr == '') {
// 			return ;
// 		}
// 		$.ajax({
// 			url : "${ctx}/channel/calcEndTime",
// 			type : 'post',
// 			dataType : 'json',
// 			async : false,
// 			data : {"monthsOfUse" : monthsOfUse, "beginTimeStr" : beginTimeStr},
// 			success : function(data) {
// 				if (data.result == 'success') {
// 					$("#endTimeStr").text(data.data);
// 				}
// 			}
// 		});
// 	});
</script>
